<?php
/*
 * @Author: 莫莫
 * @Phone: 13450159681
 * @Description: 
 * @Date: 2024-10-22 17:06:17
 * @LastEditTime: 2025-02-22 19:23:34
 */

namespace app\index\controller;

use think\exception\ValidateException;
use app\model\Store;
use app\model\StoreImage;
use app\model\Users;
use app\model\Member;
use app\model\Config;
use think\facade\Db;

class StoreController extends Base
{
    public function list()
    {
        $config = Config::getconfig();
        if (input('get.v') != $config['program_version']) {
            $stid = input('post.stid');
            $cid = input('post.cid');

            if (empty($stid)) {
                $stid = input('get.stid');
            }
            if (empty($cid)) {
                $cid = input('get.cid');
            }
            $keyword = input('post.keyword');

            $longitude = input('post.longitude'); //经度信息
            $latitude = input('post.latitude'); //纬度信息
            if (!empty($longitude) && !empty($latitude)) {
                $sql = "select * from (select id,stid,cate_ids,weid,sort,title,owner_name,region_name,address,store_logo,status, ROUND(6378.138*2*ASIN(SQRT(POW(SIN(($latitude*PI()/180-`latitude`*PI()/180)/2),2)+COS($latitude*PI()/180)*COS(`latitude`*PI()/180)*POW(SIN(($longitude*PI()/180-`longitude`*PI()/180)/2),2)))*1000) AS distance from " . (new Store)->getTable() . " order by distance ) as a where a.distance<=" . ($config['storedistance'] * 1000) . " and status=1";
            } else {
                $sql = "select * from " . (new Store)->getTable() . " where status=1";
            }

            // $sql = "select * from " . (new Store)->getTable() . " where status=1";
            $sql .=  " and `weid` = " . weid();
            if (trim($keyword)) {
                $sql .=  " and `title` LIKE '%" . $keyword . "%'";
            }

            if (!empty($stid) && $stid != 'undefined') {
                $sql .=  " and `stid` = " . $stid;
            }

            if ($cid) {
                $sql .=  " and FIND_IN_SET('" . $cid . "',cate_ids) ";
            }

            $sql .=  " order by sort asc,id desc";

            $data = Db::query($sql);

            foreach ($data as &$vo) {
                $vo['store_logo'] = toimg($vo['store_logo']);
                $vo['distance'] = round(($vo['distance']) / 1000, 1);
            }
        }
        return $this->json(['data' => $data]);
    }

    public function detail($id)
    {
        $data = Store::find($id);

        if (!empty($data)) {
            $data = $data->toArray();
        }

        $data['store_banner'] = StoreImage::where(['sid' => $id, 'ptype' => 'banner'])
            ->field('image')
            ->order('sort asc')
            ->select()
            ->toArray();

        $data['content_img'] = StoreImage::where(['sid' => $id, 'ptype' => 'content'])
            ->field('image')
            ->order('sort asc')
            ->select()
            ->toArray();

        $data['create_time'] = time_ymd($data['create_time']);

        $data['address'] =  $data['region_name'] . $data['address'];
        if (!empty($data['environment_photo'])) {
            $data['environment_photo'] = json_decode($data['environment_photo']);
        }
        if (!empty($data['staff_photo'])) {
            $data['staff_photo'] = json_decode($data['staff_photo']);
        }
        if (!empty($data['store_photo'])) {
            $data['store_photo'] = json_decode($data['store_photo']);
        }
        return $this->json(['data' => $data]);
    }
}
